import { http } from "@/utils/http";
import type { Deployment, DeploymentList, ListOption } from "./types";
import type { BaseResponse, PromiseRes } from "@/api/response";
import {
  k8sPath,
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq
} from "./apiHelper";
import type { SourceUpdateReq } from "./protocol";

export type DeploymentQuery = ListOption & {
  // 集群名称
  clusterName?: string;
  // 命名空间
  namespace?: string;
  // 名称
  name?: string;
};

export class K8sDeployment {
  baseReq: MakePathReq = {
    src: "deployments",
    app: "apps/v1"
  };

  newEntity = (): Deployment => ({
    kind: "Deployment",
    apiVersion: this.baseReq.app,
    metadata: {
      name: "",
      namespace: "",
      labels: {},
      annotations: {}
    },
    spec: {
      selector: {
        matchLabels: {}
      },
      strategy: {
        type: "RollingUpdate",
        rollingUpdate: { maxSurge: "25%", maxUnavailable: "25%" }
      },
      template: {
        spec: {
          containers: [],
          initContainers: []
        }
      }
    }
  });

  getDeploymentList = (
    query: DeploymentQuery
  ): BaseResponse<DeploymentList> => {
    const url = k8sPath({
      src: "deployments",
      cluster: query.clusterName,
      namespace: query.namespace
    });

    return http.get(url, { params: query });
  };

  getDeploymentByNs = (query: DeploymentQuery): PromiseRes<DeploymentList> => {
    return http.get(`/api/v1/kubeHud/k8s/deployment/${query.namespace}`, {
      params: query
    });
  };

  getDeploymentDetail = (query: MergePathReq): BaseResponse<Deployment> => {
    // const url = k8sPath({
    //   src: "deployments",
    //   srcName: query.name,
    //   cluster: query.clusterName,
    //   namespace: query.namespace
    // });

    const url = k8sPathMergeReqs(query, this.baseReq);

    return http.get(url, { params: query });
  };

  updateDeployment = (
    query?: MergePathReq,
    data?: Deployment
  ): BaseResponse => {
    const url = k8sPathMergeReqs(query, this.baseReq, data);
    return http.put(url, { data });
  };

  scaleDeployment = (req: {
    name?: string;
    cluster: string;
    namespace?: string;
    replicas?: number;
    data?: Deployment;
  }): BaseResponse => {
    return http.patch("/api/v1/kubeHud/k8s/deployment/scale", {
      params: req
    });
  };

  restartDeployment = (query: DeploymentQuery): PromiseRes => {
    const restartReq = {
      spec: {
        template: {
          metadata: {
            annotations: { "kubectl.kubernetes.io/restartedAt": new Date() }
          }
        }
      }
    };
    const url = k8sPath({
      src: "deployment",
      srcName: query.name,
      cluster: query.clusterName,
      namespace: query.namespace
    });

    return http.patch(url, {
      data: restartReq
    });
  };

  createDeployment = (data: Deployment): PromiseRes => {
    const api = k8sPathMergeReqs(this.baseReq, {
      namespace: data.metadata.namespace
    });

    data.kind = "Deployment";
    data.apiVersion = this.baseReq.app;

    return http.post(api, { data });
  };

  deleteDeployment = (query: MergePathReq, data: Deployment): PromiseRes => {
    const url = k8sPathMergeReqs(query, this.baseReq);

    return http.delete(url, { data: data });
  };

  patchDeployment = (req: SourceUpdateReq<any>): BaseResponse => {
    const url = k8sPath({ src: "deployments", srcName: req.name, ...req });
    return http.patch(url, { data: req.data });
  };
}
